class_name ManualPickableItem
extends PickableItem



@onready var sprite: Sprite2D = $Sprite2D
@onready var interaction_area: InteractionArea = $InteractionArea
@onready var tip_lbl: Label = $TipLbl


func _ready() -> void:
    interaction_area.interacted.connect(pickup)
    # 玩家进入拾取范围时，高亮物品
    interaction_area.body_entered.connect(_enter_interaction_area_to_highlight.bind(true))
    # 玩家退出拾取范围时，取消物品高亮
    interaction_area.body_exited.connect(_enter_interaction_area_to_highlight.bind(false))

    setup(item_id)


func _enter_interaction_area_to_highlight(body: Node2D, flag: bool) -> void:
    if body is not Player: return

    var color = Color.WHITE * 2.5 if flag else Color.WHITE
    var tween := get_tree().create_tween()
    tween.tween_property(sprite, "modulate", color, 0.2)
    tween.play()


func setup(item_id_: int) -> void:
    item_id = item_id_
    var info := InventorySystem.get_item_info(item_id)
    # print_debug(info)
    if not info:
        sprite.texture = null
        tip_lbl.text = "未知物品"
        return
    
    sprite.texture = info.icon
    tip_lbl.text = info.name

 

func pickup() -> void:
    var info := InventorySystem.get_item_info(item_id)
    if not info: 
        printerr("无法拾取未知物品")
        return
    
    var count := 0
    match info.category:
        ItemInfo.Category.CONSUMABLE:
            count = InventorySystem.add_item(InventorySystem.consumable_props_ivt, item_id)

        ItemInfo.Category.EQUIPABLE:
            match info.equipable_slot:
                ItemInfo.EquipableSlot.COMMON_SLOT:
                    count = InventorySystem.add_item(InventorySystem.common_props_ivt, item_id)
                ItemInfo.EquipableSlot.SKILL_SLOT:
                    count = InventorySystem.add_item(InventorySystem.skill_props_ivt, item_id)
                ItemInfo.EquipableSlot.WEAPON_SLOT:
                    count = InventorySystem.add_item(InventorySystem.weapon_ivt, item_id)
    
    if count != 0:
        # TODO 提示背包已满
        pass
    else:
        ObjectPoolManager.op_manual_pi.release(self)


func set_icon_transformation(dict: Dictionary) -> void:
    for key in dict.keys():
        sprite.set(key, dict[key])